package com.bjSoft.regressionTestTool.TestSelection.ui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class RegressionTestSelectionView extends ViewPart {

	private Text percentOFSelectionText;
	private Text selectedTestCasesText;
	private Text totalTestCasesText;
	private List selectedTestCaseList;
	private List allTestCasesList;
	private HashMap<String, java.util.List<String>> data;
	private IProject project;
	public static final String ID = 
		"com.bjSoft.regressionTestTool.TestSelection.ui.RegressionTestSelectionView"; //$NON-NLS-1$

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);

		final Group testCasesGroup = new Group(container, SWT.NONE);
		final GridData gridData_4 = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gridData_4.heightHint = 137;
		gridData_4.widthHint = 451;
		testCasesGroup.setLayoutData(gridData_4);
		testCasesGroup.setText("Test Cases");
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		testCasesGroup.setLayout(gridLayout_1);

		final Label allTestCasesLabel = new Label(testCasesGroup, SWT.NONE);
		allTestCasesLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		allTestCasesLabel.setText("All Test Cases:");

		allTestCasesList = new List(testCasesGroup, SWT.V_SCROLL | SWT.BORDER);
		final GridData gridData_1 = new GridData(SWT.FILL, SWT.FILL, false, false);
		gridData_1.heightHint = 55;
		gridData_1.widthHint = 309;
		allTestCasesList.setLayoutData(gridData_1);

		final Label seleLabel = new Label(testCasesGroup, SWT.NONE);
		seleLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		seleLabel.setText("Selected Test Cases:");

		selectedTestCaseList = new List(testCasesGroup, SWT.V_SCROLL | SWT.BORDER);
		final GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gridData.heightHint = 55;
		gridData.widthHint = 197;
		selectedTestCaseList.setLayoutData(gridData);

		final Group resultsGroup = new Group(container, SWT.NONE);
		final GridData gridData_5 = new GridData(SWT.RIGHT, SWT.FILL, false, false);
		gridData_5.widthHint = 391;
		resultsGroup.setLayoutData(gridData_5);
		resultsGroup.setText("Results");
		final GridLayout gridLayout_2 = new GridLayout();
		gridLayout_2.numColumns = 2;
		resultsGroup.setLayout(gridLayout_2);

		final Label totalTestCasesLabel = new Label(resultsGroup, SWT.NONE);
		totalTestCasesLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		totalTestCasesLabel.setText("Total Test Cases:");

		totalTestCasesText = new Text(resultsGroup, SWT.BORDER);
		final GridData gridData_2 = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gridData_2.widthHint = 146;
		totalTestCasesText.setLayoutData(gridData_2);

		final Label testCaseSelectedsLabel = new Label(resultsGroup, SWT.NONE);
		testCaseSelectedsLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		testCaseSelectedsLabel.setText("Test Cases selected:");

		selectedTestCasesText = new Text(resultsGroup, SWT.BORDER);
		final GridData gridData_3 = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gridData_3.widthHint = 59;
		selectedTestCasesText.setLayoutData(gridData_3);

		final Label percentOfTotalLabel = new Label(resultsGroup, SWT.NONE);
		percentOfTotalLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		percentOfTotalLabel.setText("Percent of selection:");

		percentOFSelectionText = new Text(resultsGroup, SWT.BORDER);
		percentOFSelectionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	public void setData(HashMap<String, java.util.List<String>> data, IProject project) {
		this.data = data;
		this.project = project;
		showData();
	}
	private void showData() {
		this.selectedTestCaseList.removeAll();
		this.allTestCasesList.removeAll();
		Set<String> set = data.keySet();
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			String className = it.next();			
			//System.out.println("-> " + className + " :");
			java.util.List<String> tests = data.get(className);
			Iterator<String> it2 = tests.iterator();
			while (it2.hasNext()) {
				String temp = it2.next();
				if (notExistInTheList(temp)) {
					this.selectedTestCaseList.add(temp);				
				}
			}
		}
		IFolder folder = project.getFolder("bin");
		if (folder.exists()) {
			try {
				IResource[] members = folder.members();
				for (int i = 0; i < members.length; i++) {
					if (members[i] instanceof IFile) {
						IFile file = (IFile) members[i];
						if (file.getFileExtension().equalsIgnoreCase("class")
								&& file.getName().contains("_covCase")) {
							String testCaseName = file.getName();
							this.allTestCasesList.add(
									testCaseName.substring(0, testCaseName.length() - 6));							
						}
						
					}
				}
				
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int totalTestLength = this.allTestCasesList.getItemCount();
		int selectedTestsLength = this.selectedTestCaseList.getItemCount();
		this.totalTestCasesText.setText("" + totalTestLength);
		this.selectedTestCasesText.setText("" + selectedTestsLength);
		
		double percentage = 0.0;
		if (totalTestLength > 0) 
			percentage = ((double) selectedTestsLength)/((double)totalTestLength);
		percentage *= 100;
		this.percentOFSelectionText.setText("" + Math.round(percentage) + " %");
	}
		
	
	
	private boolean notExistInTheList(String temp) {
		String[] strs = this.selectedTestCaseList.getItems();
		for (int i = 0; i < strs.length; i++) {
			if (strs[i].equalsIgnoreCase(temp)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	public void clear() {
		this.allTestCasesList.removeAll();
		this.percentOFSelectionText.setText("");
		this.selectedTestCaseList.removeAll();
		this.selectedTestCasesText.setText("");
		this.totalTestCasesText.setText("");
		
	}

}
